package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Grade.GradeDto;
import cn.yhbk.web.modules.manager.Pojo.DTO.Major.MajorDto;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbGrade;
import cn.yhbk.web.modules.manager.Pojo.VO.Grade.GradeAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.Grade.GradeUpdate;
import cn.yhbk.web.modules.manager.Service.ITbGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 年级表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Api(tags = "年级接口")
@Slf4j
@RestController
@RequestMapping("/manager/tbGrade")
public class TbGradeController {

    @Autowired
    private ITbGradeService iTbGradeService;

    @ApiOperation("根据ID查询年级")
    @GetMapping("/getGradeInfo")
    public CommonResult<TbGrade> getGradeInfo(@RequestParam("id") Long id){
        log.info("查询年级,id:" + id);
        return CommonResult.success(iTbGradeService.getById(id));
    }

    @ApiOperation("增加年级")
    @PostMapping("/saveGrade")
    public CommonResult<String> saveGrade(@RequestBody GradeAdd gradeAdd){
        log.info("增加年级，" + gradeAdd);
        TbGrade tbGrade = BeanUtil.copyProperties(gradeAdd, TbGrade.class);
        tbGrade.setCreateTime(LocalDateTime.now());
        tbGrade.setUpdateTime(LocalDateTime.now());
        tbGrade.setStatus(1);
        Boolean saveGrade = iTbGradeService.save(tbGrade);
        if(!saveGrade){
            return CommonResult.failed("保存年级出错");
        }
        return CommonResult.success("保存成功");
    }

    @ApiOperation("修改年级")
    @PutMapping("/updateGrade")
    public CommonResult<String> updateGrade(@RequestBody GradeUpdate gradeUpdate){
        log.info("修改年级，" + gradeUpdate);
        TbGrade tbGrade = iTbGradeService.getById(gradeUpdate.getId());
        BeanUtils.copyProperties(gradeUpdate, tbGrade);
        tbGrade.setUpdateTime(LocalDateTime.now());
        Boolean updateClass = iTbGradeService.updateById(tbGrade);
        if(!updateClass){
            return CommonResult.failed("修改年级出错");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("删除年级")
    @DeleteMapping("/deleteGrade/{gradeId}")
    public CommonResult<String> deleteGrade(@PathVariable("gradeId") Long gradeId){
        log.info("删除年级，" + gradeId);
        Boolean deleteGrade= iTbGradeService.removeById(gradeId);
        if(!deleteGrade){
            return CommonResult.failed("删除年级出错");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("查询所有年级")
    @GetMapping("/getGradeAll")
    public CommonResult<List<GradeDto>> getGradeAll(){
        log.info("查询所有年级");
        List<TbGrade> gradeList = iTbGradeService.getGradeAll();

        List<GradeDto> res = gradeList.stream().map(item -> {
            GradeDto gradeDto = new GradeDto();
            gradeDto.setLabel(item.getNumber());
            gradeDto.setValue(item.getId());
            gradeDto.setChildren(new ArrayList<>());
            return gradeDto;
        }).collect(Collectors.toList());

        return CommonResult.success(res);
    }

    @ApiOperation("根据ID查询年级的专业及其人数")
    @GetMapping("/getGradeMajor/{gradeId}")
    public CommonResult<List<MajorDto>> getGradeMajor(@PathVariable("gradeId") Long gradeId){
        // TODO
        return null;
    }
}
